<template>
  <el-container class="home-el-container">
    <!--  头部区域  -->
    <el-header>
      <div>
        <img src="../assets/heima.png">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--  主体区域  -->
    <el-container>
      <!--   左侧边   -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--    菜单    -->
        <el-menu
          :default-active="activePath"
          :router="true"
          :collapse-transition="false"
          :collapse="iscollapse"
          :unique-opened="true"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff">
          <!--      一级菜单      -->
          <el-submenu :index="i.id + ''" v-for="i in menulist" :key="i.id">
            <template slot="title">
              <i :class="iconObj[i.id]"></i>
              <span>{{i.authName}}</span>
            </template>
            <!--      二级菜单      -->
            <el-menu-item :index=" '/' + j.path" v-for="j in i.children" :key="j.id"
                          @click="saveNavState('/' + j.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{j.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--   主显示区   -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>export default {
  name: 'Home',
  data () {
    return {
      // 菜单数据
      menulist: [],
      // 自定义ico
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 是否折叠
      iscollapse: false,
      // 激活的连接地址
      activePath: ''
    }
  },
  mounted () {
    // 组件初始化
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 菜单
    getMenuList () {
      this.$http.get('menus').then(res => {
        if (res.data.meta.status !== 200) return this.$message.error(res.data.mets.msg)
        this.menulist = res.data.data
      })
    },
    // 菜单折叠
    toggleCollapse () {
      this.iscollapse = !this.iscollapse
    },
    // 保存激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
  .el-header, .el-footer {
    background-color: #373D41;
    color: #333;
    text-align: center;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    > div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }

    }
  }

  .el-aside {
    background-color: #333744;
    color: #333;
    text-align: center;
    line-height: 200px;

    .el-menu {
      border: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  /* 右侧边高占100% */
  .home-el-container {
    height: 100%;
  }

  .iconfont {
    margin-right: 10px;
  }

  .toggle-button {
    background-color: #4a5064;
    font-size: 15px;
    line-height: 20px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    padding: 10px 0;
    cursor: pointer;
  }
</style>
